package com.example.chapter0;

import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.Date;

public class ButtonStyleActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView textView;

    @SuppressLint({"MissingInflatedId", "SimpleDateFormat", "ClickableViewAccessibility"})
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_button_style);
//        textView = findViewById(R.id.tv_result);
        textView = findViewById(R.id.tv_result_button);
        Button button = findViewById(R.id.btn_click_single);
        button.setOnClickListener(new MyViewClickListener(textView));
        Button publicButton = findViewById(R.id.btn_click_public);
        publicButton.setOnClickListener(this);
        Button longButton = findViewById(R.id.btn_click_long);
        longButton.setOnLongClickListener(view -> {
            SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
            String desc = String.format("%s 您点击了按钮： %s", sdf.format(new Date()), ((Button) view).getText());
            textView.setText(desc);
            Log.d("ButtonStyleActivity", "dianji");
            System.out.println(desc);
            return true;
        });
//        longButton.setOnTouchListener((view, motionEvent) -> {
//            int i = 1;
//            if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
//                System.out.println(i + 1);
//                Log.d("i",String.valueOf(i));
//            } else if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
//                System.out.println(i+2);
//                Log.d("i",String.valueOf(i));
//            }
//            return true;
//        });
    }

    @SuppressLint("SimpleDateFormat")
    public void doClick(View view) {
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        String desc = String.format("%s 您点击了按钮： %s", sdf.format(new Date()), ((Button) view).getText());
        textView.setText(desc);

    }

    @Override
    @SuppressLint("SimpleDateFormat")
    public void onClick(View view) {
        if (view.getId() == R.id.btn_click_public) {
            SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
            String desc = String.format("%s 您点击了按钮： %s", sdf.format(new Date()), ((Button) view).getText());
            textView.setText(desc);

        }

    }

    static class MyViewClickListener implements View.OnClickListener {
        private final TextView textView;

        public MyViewClickListener(TextView textView) {
            this.textView = textView;
        }

        @SuppressLint("SimpleDateFormat")
        @Override
        public void onClick(View view) {
            SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
            String desc = String.format("%s 您点击了按钮： %s", sdf.format(new Date()), ((Button) view).getText());
            textView.setText(desc);
        }
    }
}